package collection.mymap.concurrent;

import org.testng.annotations.Test;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.*;

public class ConcurrentMap {

    @Test
    private void t1() {
//        Map<Object, Object> synchronizedMap = Collections.synchronizedMap();

        var map = new ConcurrentHashMap<>();
        map.put(1, 1);
        // 17.58+0.7+0.7-1.5-0.6-1.94-13.7
        // 15.58+0.7+0.7-1-0.6-2.1-13.7

        System.out.println(map);
    }

    public static void main(String[] args) {
        var map = new ConcurrentHashMap<>();

        // 使用线程池会导致程序不退出？
        // 因为线程池的核心线程是不死的，所以进程里一直有线程存在
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 12, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>());


        Runnable r = () -> {
            for (int i = 0; i < 1000; i++) {
                map.put(i, i);
                System.out.println(Thread.currentThread().getName() + " " + i);
                map.get(i);
            }
        };

        Runnable r2 = () -> {
            System.out.println("r2");
        };

        threadPoolExecutor.execute(r2);

        while(true) {

        }

    }

}
